package com.lbm.business.web.utils;

import java.util.List;
import java.util.Map;
import com.lbm.baseclass.plugins.jxl.BaseExport;
import com.lbm.baseclass.plugins.jxl.ExcelExportException;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * <p> Title:查询结果导出到excel文件中保存 </p>
 * <p> Description:</p>
 * <p> Copyright: Copyright (c) 3011 </p>
 * <p> Company:ssa.co.,ltd </p>
 *
 * @author zhuq
 * @version 1.0
 */
public class ExcelExportHelper extends BaseExport {
	public WritableFont FONT = new WritableFont(WritableFont.ARIAL, 10);
	public WritableCellFormat STRING_FORMAT = null;
	public WritableCellFormat NUMBER_FORMAT = null;
	public ExcelExportHelper(WritableWorkbook wbook) {
		super(wbook);
		try {
			STRING_FORMAT = new WritableCellFormat(FONT);
			STRING_FORMAT.setBorder(Border.ALL, BorderLineStyle.THIN);
			STRING_FORMAT.setVerticalAlignment(VerticalAlignment.CENTRE);
			STRING_FORMAT.setAlignment(Alignment.CENTRE);
			STRING_FORMAT.setWrap(true);

			NUMBER_FORMAT = new WritableCellFormat(FONT);
			NUMBER_FORMAT.setBorder(Border.ALL, BorderLineStyle.THIN);
			NUMBER_FORMAT.setVerticalAlignment(VerticalAlignment.CENTRE);
			NUMBER_FORMAT.setAlignment(Alignment.RIGHT);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 把查询出的结果导出到一个临时的EXCEL文件
	 * @param list
	 * @param sheetName
	 * @throws ExcelExportException
	 */
	public void writeProductinoScore(List<Map<String,Object>> list, String sheetName)
			throws ExcelExportException {
		if (!list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				Map<String,Object> map = list.get(i);
				addNumber(0, i + 3, (i + 1));// 序号生成规则
				addString(1, i + 3, (String)map.get("USER_NOTE"));
				addString(2, i + 3, (String)map.get("CLASS"));
				addString(3, i + 3, (String)map.get("PRODUCTION_NAME"));
				String score=(String)map.get("SCORE");
				if(score==""||score==null)
					addString(4, i + 3, "");
				else
					addNumber(4, i + 3, Float.parseFloat(score));
			}
		}
	}
	public boolean addString(int column, int row, String label)
			throws ExcelExportException {
		WritableSheet sheet = this.getWbook().getSheet(0);
		if (sheet != null) {
			if (label==""||label==null) {
				label = " ";
			}
			Label lbl = new Label(column, row, label, STRING_FORMAT);
			try {
				sheet.addCell(lbl);
			} catch (Exception e) {
			}
			return true;
		}
		return false;
	}

	public boolean addNumber(int column, int row, double number) {
		WritableSheet sheet = this.getWbook().getSheet(0);
		if (sheet != null) {
			jxl.write.Number labelN = new jxl.write.Number(column, row, number,
					NUMBER_FORMAT);
			try {
				sheet.addCell(labelN);
				return true;
			} catch (Exception e) {
			}
		}
		return false;
	}
}
